package com.kexio.common.config;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 缓存配置属性
 * 
 * 🎯 设计理念：灵活配置，运维友好
 * - 支持默认配置和特定缓存配置
 * - 支持不同类型缓存的差异化TTL设置
 * - 配置变更无需重启应用
 * 
 * 📝 配置示例：
 * ```yaml
 * kexio:
 *   cache:
 *     default:
 *       ttl: 30m
 *       maxIdleTime: 15m
 *       maxSize: 10000
 *     configs:
 *       userCache:
 *         ttl: 2h
 *         maxIdleTime: 30m
 *         maxSize: 5000
 * ```
 * 
 * @author Kexio Team
 * @since 2.0.0
 */
@Component
@ConfigurationProperties(prefix = "kexio.cache")
public class CacheConfigProperties {
    
    /**
     * 默认缓存配置
     */
    private CacheSpec defaultConfig = new CacheSpec();
    
    /**
     * 特定缓存配置，键为缓存名称
     */
    private Map<String, CacheSpec> configs = new HashMap<>();
    
    /**
     * 获取指定缓存的配置，不存在则返回默认配置
     * 
     * @param cacheName 缓存名称
     * @return 缓存配置
     */
    public CacheSpec getConfig(String cacheName) {
        return configs.getOrDefault(cacheName, defaultConfig);
    }
    
    // Getters and Setters
    public CacheSpec getDefaultConfig() {
        return defaultConfig;
    }
    
    public void setDefaultConfig(CacheSpec defaultConfig) {
        this.defaultConfig = defaultConfig;
    }
    
    public Map<String, CacheSpec> getConfigs() {
        return configs;
    }
    
    public void setConfigs(Map<String, CacheSpec> configs) {
        this.configs = configs;
    }
    
    /**
     * 缓存规格配置
     */
    public static class CacheSpec {
        /**
         * 存活时间
         */
        private Duration ttl = Duration.ofMinutes(30);
        
        /**
         * 最大空闲时间
         */
        private Duration maxIdleTime = Duration.ofMinutes(15);
        
        /**
         * 最大缓存大小
         */
        private Integer maxSize = 10000;
        
        /**
         * 是否启用本地缓存
         */
        private Boolean enableLocalCache = true;
        
        // Getters and Setters
        public Duration getTtl() {
            return ttl;
        }
        
        public void setTtl(Duration ttl) {
            this.ttl = ttl;
        }
        
        public Duration getMaxIdleTime() {
            return maxIdleTime;
        }
        
        public void setMaxIdleTime(Duration maxIdleTime) {
            this.maxIdleTime = maxIdleTime;
        }
        
        public Integer getMaxSize() {
            return maxSize;
        }
        
        public void setMaxSize(Integer maxSize) {
            this.maxSize = maxSize;
        }
        
        public Boolean getEnableLocalCache() {
            return enableLocalCache;
        }
        
        public void setEnableLocalCache(Boolean enableLocalCache) {
            this.enableLocalCache = enableLocalCache;
        }
        
        @Override
        public String toString() {
            return "CacheSpec{" +
                    "ttl=" + ttl +
                    ", maxIdleTime=" + maxIdleTime +
                    ", maxSize=" + maxSize +
                    ", enableLocalCache=" + enableLocalCache +
                    '}';
        }
    }
}

